<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>客户汽运协议运价</title>
        <% include("/common/common_css_js.html"){} %>
    </head>
    <body>
        <div style="padding-bottom: 5px;">
            <div id="queryForm" class="uc-toolbaroverflow" _borderStyle="border-top:0;">
                <span class="separator"></span>
                <span>客户:</span>
                <div id="keyDeliveryCustomerId" name="keyDeliveryCustomerId" class="mini-autocomplete" style="width:250px;"  popupWidth="1000" textField="name" valueField="id"
                     url="${base}/pages/customer_manage/queryDeliveryCustomerByKey"  required="false" requiredErrorText="客户不能为空"
                     searchField="key"
                     emptyText="客户名称">
                    <div property="columns">
                        <div header="客户名称" field="name"></div>
                        <div header="客户简称" field="shortName"></div>
                        <div header="客户电话" field="phone"></div>
                        <div header="客户地址" field="address"></div>
                    </div>
                </div>

                <span>目的站：</span>
                <input id="keyCityId" name="keyCityId" class="mini-treeselect"
                       url="" multiSelect="false" valueField="id" textField="name"
                       parentField="pid" allowInput="true" valueFromSelect="true"
                       emptyText="请选择目的站..."
                       style="width: 200px;" expandOnLoad="0" required="false" nullItemText="请选择目的站..."/>

                <div style="margin-top: 5px"></div>
                <% if(so.hasPermission("bCustomerAutoFreightAgreement:query")){%>
                <a class="mini-button" iconCls="icon-search" onclick="search()">查询</a>
                <% } %>

                <% if(so.hasPermission("bCustomerAutoFreightAgreement:queryCndClear")){%>
                <a class="mini-button" iconCls="icon-new" onclick="clearSearch()">清空查询条件</a>
                <% } %>

                <span class="separator"></span>

                <% if(so.hasPermission("bCustomerAutoFreightAgreement:add")){%>
                <a class="mini-button" iconCls="icon-add" onclick="addAuto('add')">新增</a>
                <% } %>

                <% if(so.hasPermission("bCustomerAutoFreightAgreement:edit")){%>
                <a class="mini-button" iconCls="icon-edit" onclick="editAuto('edit')">编辑</a>
                <% } %>

                <% if(so.hasPermission("bCustomerAutoFreightAgreement:delete")){%>
                <a class="mini-button" iconCls="icon-remove" onclick="deleteAuto()">删除</a>
                <% } %>

                <span class="separator"></span>
                <% if(so.hasPermission("bCustomerAutoFreightAgreement:upload")){%>
                <a class="mini-button" iconCls="icon-upload" onclick="showUploadPanel()">上传</a>
                <% } %>

                <a href="${base!}/hymj/files/汽运协议运价.rar" target="_blank">点击下载《汽运协议运价》模板</a>
            </div>
        </div>

        <div class = "mini-fit">
            <div id="datagrid1" class="mini-datagrid"
                 style="width: 100%; height: 100%;"
                 url="${base}/pages/customer_manage/freight_agreement/auto/queryAutoFreightAgreementList" idField="id"
                 showPager="true" pageSize="20" sizeList="[10,20,30,50,100]"
                 multiSelect = "true" allowResize="true"
                 allowSortColumn = "true"
                 sortField="createTime"
                 sortOrder="desc" >
                <div property="columns">
                    <div type="checkcolumn" ></div>
                    <div field="customerId" width="140" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"customer"}' renderer="commonRender">客户名称</div>
                    <div field="cityId" width="140" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"city"}' renderer="commonRender">目的站</div>
                    <div field="comment" width="200" headerAlign="center" align="center" allowSort="true">备注</div>
                    <div field="createTime" width="140" headerAlign="center" align="center" allowSort="false" dateFormat="yyyy-MM-dd HH:mm:ss" renderer="ondayRenderer">创建时间</div>
                    <div field="createUserId" width="140" headerAlign="center" align="center" allowSort="false" data-options='{cacheName:"sysUser"}' renderer="commonRender">创建用户</div>
                    <div field="updateTime" width="140" headerAlign="center" align="center" allowSort="false" dateFormat="yyyy-MM-dd HH:mm:ss" renderer="ondayRenderer">更新时间</div>
                    <div field="updateUserId" width="140" headerAlign="center" align="center" allowSort="false" data-options='{cacheName:"sysUser"}' renderer="commonRender">更新用户</div>
                </div>
            </div>
        </div>


        <!-- 新增航空协议运价窗口 -->
        <div id="autoFreightAgreementPanel" class="mini-window" title="客户汽运协议运价管理" style="width:1300px;height:500px;"
             showMaxButton="true" showCollapseButton="false" showMinButton="true" showShadow="true"
             showToolbar="true" showFooter="true" showModal="true" allowResize="true" allowDrag="true">

            <div property="footer" style="padding:10px;text-align: right">
                <a  class="mini-button" iconCls="icon-ok" onclick="saveAutoFreightAgreement()">保存</a>
                <a  class="mini-button" iconCls="icon-close" onclick="closePanel('autoFreightAgreementPanel')">关闭</a>
            </div>


            <div id="freightAgreementForm" style="width: 100%">

                <input id="opFlag" name="opFlag" emptyText="操作标志"  class="mini-hidden" required="false" enabled="false" enabled="false" requiredErrorText="操作标志不能为空" />
                <input id="id" name="id" emptyText="id值"  class="mini-hidden" required="false" enabled="false" enabled="false" requiredErrorText="ID不能为空" />

                <span>客户:</span>
                <div id="customerId" name="customerId"
                     class="mini-combobox"
                     style="width:250px;"
                     popupWidth="800" textField="name" valueField="id"
                     allowInput="true" required="true" requiredErrorText="客户不能为空"
                     emptyText="客户名称">
                    <div property="columns">
                        <div header="客户名称" field="name"></div>
                        <div header="客户简称" field="shortName"></div>
                        <div header="客户电话" field="phone"></div>
                        <div header="客户地址" field="address"></div>
                    </div>
                </div>

                <span>目的站：</span>
                <input id="cityId" name="cityId" class="mini-treeselect"
                       url="" multiSelect="false" valueField="id" textField="name"
                       parentField="pid" allowInput="true" valueFromSelect="true"
                       style="width: 200px;" expandOnLoad="0" required="true" nullItemText="请选择..." emptyText="请选择..."/>

                <span>起票价：</span>
                <input id="ticketPrice" name="ticketPrice" emptyText="请填写起票价" vtype="float" value="0" class="mini-textbox"  required="true" requiredErrorText="起票价不能为空" />

                <div style="margin-top: 5px"></div>

                <span >备注:</span>
                <input id="comment" name="comment" emptyText="请输入备注信息"  class="mini-textbox" style="width: 550px"  required="false" requiredErrorText="备注信息" />

                <div style="margin-top: 5px"></div>


                <div style="display:flex;flex-direction: row;width:100%;height: 365px;">
                    <div style="display: flex;flex-direction:column; justify-content: center; width: 50%;height: 100%;">
                        <div>重量运价标准</div>
                        <div style="margin-top: 5px"></div>
                        <div style="display: flex;flex-direction: row;align-items:center;width: 100%">
                            <input id="weightGoodsTypeId" name="weightGoodsTypeId" class="mini-combobox" style="width:100px;" textField="name" valueField="id" emptyText="货物类型..."  required="false" allowInput="false" showNullItem="false" nullItemText="请选择货物类型..." requiredErrorText="没有选择货物类型"/>
                            <span style="margin-right: 2px"></span>
                            <input id="weightStart" name="weightStart" class="mini-textbox"  emptyText="重量起始值" vtype="float" style="width: 80px" />
                            <span style="margin-right: 2px"></span>
                            <input id="weightEnd" name="weightEnd" class="mini-textbox"  emptyText="重量结束值" vtype="float" style="width: 80px" />
                            <span style="margin-right: 2px"></span>
                            <input id="weightPrice" name="weightPrice" class="mini-textbox" emptyText="单价(元/公斤)" vtype="float" style="width: 100px" />
                            <span style="margin-right: 2px"></span>
                            <input id="weightComment" name="weightComment" class="mini-textbox" emptyText="说明" style="width: 80px" />
                            <span class="separator"></span>
                            <a class="mini-button" onclick="addWeightDatagrid()">添加</a>
                            <span style="margin-right: 2px"></span>
                            <a class="mini-button" onclick="deleteWeightDatagrid()">删除</a>
                        </div>
                        <div style="margin-top: 5px"></div>
                        <div id="weightDatagrid" class="mini-datagrid" style="width:100%;height:310px;"
                             idField="id"
                             showPager="false"
                             allowCellEdit="true" allowCellSelect="true"  multiSelect="true">
                            <div property="columns">
                                <div type="checkcolumn"></div>
                                <div field="goodsTypeId" width="50" headerAlign="center" align="center" data-options='{cacheName:"sysDic"}' renderer="commonRender">货物类型</div>
                                <div field="startValue" width="70" headerAlign="center" align="right" renderer="public_moneyRenderer">重量起始值</div>
                                <div field="endValue" width="70" headerAlign="center" align="right" renderer="public_moneyRenderer">重量结束值</div>
                                <div field="price" width="80" headerAlign="center" align="right" renderer="public_moneyRenderer">单价(元/公斤)</div>
                                <div field="comment" width="140" headerAlign="center" align="left">说明</div>
                                <div field="standardTypeId" width="0">标准类型</div>
                            </div>
                        </div>
                    </div>
                    <div style="margin-left: 2px;margin-right: 2px"></div>
                    <div style="display: flex;flex-direction:column;align-content: center;justify-content: center;width: 50%;height: 100%">
                        <div>体积运价标准</div>
                        <div style="margin-top: 5px"></div>
                        <div style="display: flex;flex-direction: row;align-items:center;width: 100%">
                            <input id="volumeGoodsTypeId" name="volumeGoodsTypeId" class="mini-combobox" style="width:100px;" textField="name" valueField="id" emptyText="货物类型..."  required="false" allowInput="false" showNullItem="false" nullItemText="请选择货物类型..." requiredErrorText="没有选择货物类型"/>
                            <span style="margin-right: 2px"></span>
                            <input id="volumeStart" name="volumeStart" class="mini-textbox"  emptyText="体积起始值" vtype="float" style="width: 80px" />
                            <span style="margin-right: 2px"></span>
                            <input id="volumeEnd" name="volumeEnd" class="mini-textbox"  emptyText="体积结束值" vtype="float" style="width: 80px" />
                            <span style="margin-right: 2px"></span>
                            <input id="volumePrice" name="volumePrice" class="mini-textbox"  emptyText="单价(元/公斤)" vtype="float" style="width: 100px" />
                            <span style="margin-right: 2px"></span>
                            <input id="volumeComment" name="weightComment" class="mini-textbox" emptyText="说明" style="width: 80px" />
                            <span class="separator"></span>
                            <a class="mini-button" onclick="addVolumeDatagrid()">添加</a>
                            <span style="margin-right: 2px"></span>
                            <a class="mini-button" onclick="deleteVolumeDatagrid()">删除</a>
                        </div>
                        <div style="margin-top: 5px"></div>
                        <div id="volumeDatagrid" class="mini-datagrid" style="width:100%;height:310px;"
                             idField="id"
                             showPager="false"
                             allowCellEdit="true" allowCellSelect="true"  multiSelect="true">
                            <div property="columns">
                                <div type="checkcolumn"></div>
                                <div field="goodsTypeId" width="50" headerAlign="center" align="center" data-options='{cacheName:"sysDic"}' renderer="commonRender">货物类型</div>
                                <div field="startValue" width="70" headerAlign="center" align="right" renderer="public_moneyRenderer">体积起始值</div>
                                <div field="endValue" width="70" headerAlign="center" align="right" renderer="public_moneyRenderer">体积结束值</div>
                                <div field="price" width="80" headerAlign="center" align="right" renderer="public_moneyRenderer">单价(元/公斤)</div>
                                <div field="comment" width="140" headerAlign="center" align="left">说明</div>
                                <div field="standardTypeId" width="0">标准类型</div>
                            </div>
                        </div>
                    </div>
                </div>

            </div>
        </div>

        <!-- 批量上传协议运价 -->
        <div id="uploadPanel" class="mini-window" title="协议运价上传" style="width:400px;height:200px;"
             showMaxButton="true" showCollapseButton="false" showMinButton="true" showShadow="true"
             showToolbar="true" showFooter="true" showModal="true" allowResize="true" allowDrag="true" >
            <div property="footer" style="padding:10px;text-align: right">
                <a  id="uploadBtn" name="uploadBtn" class="mini-button" iconCls="icon-upload" onclick="uploadSave()">上传</a>
            </div>
            <table style="width: 100%">
                <tr>
                    <td><label>客户协议运价文件:</label></td>
                    <td>
                        <form id="upload-form-add">
                            <input type="file" id="upLoadStandardFileData" name="upLoadFileData"  accept=".xls,.xlsx" />
                        </form>
                    </td>
                </tr>
            </table>
        </div>

        <script type="text/javascript">
            mini.parse();

            let keyDeliveryCustomerId = mini.get("keyDeliveryCustomerId");
            let keyCityId = mini.get("keyCityId");

            let grid = mini.get("datagrid1");

            let autoFreightAgreementPanel = mini.get("autoFreightAgreementPanel");
            let freightAgreementForm = new mini.Form("#freightAgreementForm");
            let opFlag = mini.get("opFlag");
            let customerId = mini.get("customerId");
            let cityId = mini.get("cityId");
            let comment = mini.get("comment");

            // 重量标准
            let weightGoodsTypeId = mini.get("weightGoodsTypeId");
            let weightStart = mini.get("weightStart");
            let weightEnd = mini.get("weightEnd");
            let weightPrice = mini.get("weightPrice");
            let weightComment = mini.get("weightComment");

            let weightDatagrid = mini.get("weightDatagrid");
            let weightDatagridList = [];
            let weightGeneralArray = [];
            let weightFreshArray = [];

            // 体积标准
            let volumeGoodsTypeId = mini.get("volumeGoodsTypeId");
            let volumeStart = mini.get("volumeStart");
            let volumeEnd = mini.get("volumeEnd");
            let volumePrice = mini.get("volumePrice");
            let volumeComment = mini.get("volumeComment");

            let volumeDatagrid = mini.get("volumeDatagrid");
            let volumeDatagridList = [];
            let volumeGeneralArray = [];
            let volumeFreshArray = [];

            /**
             * 40,0,客户协议运价标准分类,客户协议运价标准分类,XT_DIC_CUSTOMER_AGREEMENT_FREIGHT_STANDARD
             41,40,重量运价标准,重量运价标准,""
             42,40,体积运价标准,体积运价标准,""
             */
            let freightStandard = {weight:41, volume:42};

            /**
             * 50,0,货物类型,货物类型,XT_DIC_GOODS_TYPE
             51,50,普货,普货,""
             52,50,鲜活,鲜活,""
             */
            let goodsType = {general:51, fresh:52};

            let transportSpecies ={air:71, auto:72};

            $(function () {
                //  目的市
                let keyCityIdJsonData = commonRenderDropList("city");
                keyCityId.loadList(keyCityIdJsonData,"id","pid");

                // 加载列表数据
                grid.load();
            });

            /**
             * 查询
             */
            function search(){
                let form = new mini.Form("#queryForm");
                let data = form.getData(true);
                grid.load(data);
            }

            /**
             * 清空查询
             */
            function clearSearch(){
                let form = new mini.Form("#queryForm");
                form.clear();
            }

            function panelInit(){
                //  目的市
                let cityIdJsonData = commonRenderDropList("city");
                cityId.loadList(cityIdJsonData,"id","pid");
                // 客户加载
                let customerIdJsonData = commonRenderDropList("customer");
                customerId.load(customerIdJsonData);

                let weightGoodsTypeIdJsonData = commonRenderSysDicDropListByKey("sysDic","XT_DIC_GOODS_TYPE");
                weightGoodsTypeId.load(weightGoodsTypeIdJsonData);
                volumeGoodsTypeId.load(public_deepClone(weightGoodsTypeIdJsonData));

                public_showModelAtPos("autoFreightAgreementPanel", "center", "middle");
            }

            /**
             * 添加协议运价明细
             * @param opType
             */
            function addAuto(opFlagValue){
                emptyValue();
                opFlag.setValue(opFlagValue);
                panelInit();
            }

            /**
             * 编辑协议运价明细
             * @param opTypeValue
             */
            function editAuto(opFlagValue){
                emptyValue();

                let selectItem = grid.getSelected();

                if(public_isEmpty(selectItem)){
                    showAlert("请选择需要编辑的协议运价!");
                    return;
                }

                if(selectItem.length>1){
                    showAlert("编辑协议运价不能选择多行，请选择单行!");
                    return;
                }

                panelInit();

                freightAgreementForm.setData(selectItem);

                // 一定要放到freightAgreementForm.setData之后
                opFlag.setValue(opFlagValue);

                // 加载相应的detail数据，根据agreement_freight_id
                let ret = public_getServerData("${base}/pages/customer_manage/freight_agreement/detail/getDetailListByAgreementFreightId?agreementFreightId=" + selectItem.id);
                if(!ret.ok){
                    showAlert(ret.message);
                    return;
                }
                // 可以显示数据，但在显示数据之前需要分离数据

                // 先清空一下
                emptyDataAndGrid();

                // 开始分离数据
                let allDataList = ret.data;
                for(let obj of allDataList){
                    // 根据standard_type_id 分离是重量还是体积
                    if(obj.standardTypeId == freightStandard.weight){
                        if(obj.goodsTypeId == goodsType.general){
                            weightGeneralArray.push(public_deepClone(obj));
                        }
                        else{
                            weightFreshArray.push(public_deepClone(obj));
                        }
                    }
                    else {
                        if(obj.goodsTypeId == goodsType.general){
                            volumeGeneralArray.push(public_deepClone(obj));
                        }
                        else{
                            volumeFreshArray.push(public_deepClone(obj));
                        }
                    }
                }

                dataListSort();
                weightDatagridList.push(...weightGeneralArray);
                weightDatagridList.push(...weightFreshArray);
                volumeDatagridList.push(...volumeGeneralArray);
                volumeDatagridList.push(...volumeFreshArray);

                // 开始渲染grid
                for(let obj of weightDatagridList){
                    weightDatagrid.addRow(obj);
                }

                for(let obj of volumeDatagridList){
                    volumeDatagrid.addRow(obj);
                }


            }

            function dataListSort() {
                if(weightDatagridList.length>1){
                    weightDatagridList.sort(sortData);
                }

                if(weightGeneralArray.length>1){
                    weightGeneralArray.sort(sortData);
                }

                if(weightFreshArray.length>1){
                    weightFreshArray.sort(sortData);
                }


                if(weightDatagridList.length>1){
                    weightDatagridList.sort(sortData);
                }

                if(volumeDatagridList.length>1){
                    volumeDatagridList.sort(sortData);
                }

                if(volumeGeneralArray.length>1){
                    volumeGeneralArray.sort(sortData);
                }

                if(volumeFreshArray.length>1){
                    volumeFreshArray.sort(sortData);
                }
            }

            /**
             * 删除协议运价
             */
            function deleteAuto(){
                let selectItems = grid.getSelecteds();

                console.log(selectItems);

                if(public_isEmpty(selectItems)){
                    showAlert("请选择需要删除的协议运价!");
                    return;
                }

                let agreementFreightIds = "";
                for(let obj of selectItems){
                    agreementFreightIds += "'" + obj.id + "',";
                }

                // 去除最后一个 "," 号
                agreementFreightIds = agreementFreightIds.substring(0,agreementFreightIds.length-1);


                mini.confirm("确定要删除选择的协议运价？","协议运价删除",function (action) {
                    if (action == "ok") {
                        // 调用删除动作进行删除
                        let ret = public_getServerData("${base}/pages/customer_manage/freight_agreement/auto/delete",{agreementFreightIds:agreementFreightIds});
                        showAlert(ret.message,function () {
                            if(ret.ok){
                                grid.reload();
                            }
                        });
                    }
                });
            }


            /**
             * 上传协议运价明细
             */
            function showUploadPanel(){
                let form_add = new mini.Form("#upload-form-add");
                form_add.clear();
                // 弹出添加窗口
                public_showModelAtPos("uploadPanel", "center", "middle");
            }

            /**
             * 上传模板文件
             */
            function uploadSave() {
                let fileInput = $('#upLoadStandardFileData').get(0).files[0];
                if(fileInput){
                    mini.confirm("您确认上传客户协议运价数据?", "提示", function(confirmAction){
                        if(confirmAction == "ok"){
                            let messageid = mini.loading("正在上传校验及上传处理中，请稍后...","客户协议运价Excel上传");
                            standardAjaxFileUpload(messageid);
                        }
                    });
                }else{
                    mini.alert("没有选择要上传的航班信息数据Excel文件!");
                }
            }


            // 调用异步上传
            function standardAjaxFileUpload(messageid) {
                let formData = new FormData($("#upload-form-add")[0]);
                $.ajax({
                    //接口地址
                    url: '${base}/pages/customer_manage/freight_agreement/auto/upload' ,
                    type: 'POST',
                    data: formData,
                    async: false,
                    cache: false,
                    contentType: false,
                    processData: false,
                    success: function (data) {

                        mini.hideMessageBox(messageid);

                        //成功的回调
                        mini.alert(data.message);

                        // 重新刷新数据
                        grid.reload();

                        // 清空上传对象
                        clearUploadFile("upload-form-add");

                        public_onCancel('uploadPanel');

                    },
                    error: function (returndata) {
                        mini.alert("网络异常，请重试！");
                    }
                });
            }

            /**
             * 保存协议运价
             */
            function saveAutoFreightAgreement(){
                freightAgreementForm.validate();
                if (freightAgreementForm.isValid() == false) {
                    return;
                }

                let formData = freightAgreementForm.getData(true);

                if(weightDatagridList.length>0 || volumeDatagridList.length>0){

                    let detailPojos = [];
                    detailPojos.push(...weightDatagridList);
                    detailPojos.push(...volumeDatagridList);

                    formData.detailPojos = detailPojos;

                    // b_customer_auto_freight_agreement(这是主表)
                    // b_customer_feight_agreement_detail(这是明细表)

                    // 这个地方就不用再做校验了，因为在操作过程中已经做了完整的校验

                    let saveUrl = "${base}/pages/customer_manage/freight_agreement/auto/saveAutoAgreement";
                    let ret = public_getServerData(saveUrl, formData);
                    if(ret.ok){
                        showAlert(ret.message,function (){
                            // 清空
                            emptyValue();

                            // 刷新grid
                            grid.reload();

                            // 关闭窗口
                            closePanel('autoFreightAgreementPanel');
                        });
                    }
                    else{
                        showAlert(ret.message,function (){
                            return;
                        });
                    }
                }
                else{
                    showAlert("重量运价标准与体积运价标准都为空，不能进行保存操作!",function (){
                        return;
                    });
                }
            }


            /**
             * 添加重量分类协议运价
             */
            function addWeightDatagrid(){

                if(isEmpty(weightGoodsTypeId,"货物类型")){
                    return;
                }

                if(isEmpty(weightStart,"重量起始值")){
                    return;
                }

                if(isEmpty(weightEnd,"重量结束值")){
                    return;
                }

                if(isEmpty(weightPrice,"重量单价")){
                    return;
                }

                if(isNonNumeric(weightStart,"重量起始值")){
                    return;
                }

                if(isNonNumeric(weightEnd,"重量结束值")){
                    return;
                }

                if(isNonNumeric(weightPrice,"重量单价")){
                    return;
                }

                let weightGoodsTypeIdValue = weightGoodsTypeId.getValue();
                let weightStartValue = public_toNumber(weightStart.getValue());
                let weightEndValue = public_toNumber(weightEnd.getValue());
                let weightPriceValue = public_toNumber(weightPrice.getValue());
                let commentValue = weightComment.getValue();
                let standardTypeIdValue = freightStandard.weight;


                if(weightEndValue<=0){
                    showAlert("重量结束值不能小于或等于零!");
                    return;
                }
                else if(weightEndValue<=weightStartValue){
                    showAlert("重量结束值不能小于或等于重量起始值!");
                    return;
                }
                if(weightPriceValue<=0){
                    showAlert("重量单价不能小于或等于零!");
                    return;
                }

                // 生成新添加的对象
                let newRow = {goodsTypeId:weightGoodsTypeIdValue, startValue:weightStartValue, endValue:weightEndValue, price:weightPriceValue, comment:commentValue,standardTypeId:standardTypeIdValue, agreementFreightTypeId:transportSpecies.auto };

                if(weightDatagridList.length==0){
                    weightDatagrid.addRow(newRow);
                    weightDatagridList.push(newRow);

                    if(newRow == goodsType.general){
                        weightGeneralArray.push(newRow);
                    }
                    else{
                        weightFreshArray.push(newRow);
                    }
                    return;
                }

                // 拼接函数(索引位置, 要删除元素的数量, 元素) array.splice(2, 0, "three");
                // let goodsType = {general:51, fresh:52};
                // let freightStandard = {weight:41, volume:42};
                // weightGoodsTypeIdValue

                weightGeneralArray.splice(0,weightGeneralArray.length);
                weightFreshArray.splice(0,weightFreshArray.length);

                for(let obj of weightDatagridList){
                    if(obj.goodsTypeId == goodsType.general){
                        weightGeneralArray.push(obj);
                    }
                    else{
                        weightFreshArray.push(obj);
                    }
                }

                // 这个地方要检查在现在的列表中现有的对象是否已经存在
                if(weightGoodsTypeIdValue == goodsType.general){
                    let repetitionFlag = checkRepetition(weightGeneralArray,newRow);
                    if(!repetitionFlag){
                        weightGeneralArray.push(newRow);
                    }
                    else{
                        showAlert("新添加的运价标准在现有运价标准中已经存在,不能重复添加!");
                        return;
                    }
                }
                else{
                    let repetitionFlag = checkRepetition(weightFreshArray,newRow);
                    if(!repetitionFlag){
                        weightFreshArray.push(newRow);
                    }
                    else{
                        showAlert("新添加的运价标准在现有运价标准中已经存在,不能重复添加!");
                        return;
                    }
                }

                // 两个数组分别按起始值进行排序
                if(weightGeneralArray.length>1){
                    weightGeneralArray.sort(sortData);
                }
                if(weightFreshArray.length>1){
                    weightFreshArray.sort(sortData);
                }

                // 检测是否符合对应的要求
                if(weightGeneralArray.length>=2){
                    let weightGeneralCheckFlag = sequenceCheck(weightGeneralArray);
                    if(!weightGeneralCheckFlag){
                        showAlert("新添加的运价标准不符合前一条重量结束值与下一条重量起始值相同的要求!");
                        return;
                    }
                }
                if(weightFreshArray.length>=2){
                    let weightFreshCheckFlag = sequenceCheck(weightFreshArray);
                    if(!weightFreshCheckFlag){
                        showAlert("新添加的运价标准不符合前一条体积结束值与下一条体积起始值相同的要求!");
                        return;
                    }
                }

                // 清空数组
                weightDatagridList.splice(0,weightDatagridList.length);

                weightDatagridList.push(...weightGeneralArray);
                weightDatagridList.push(...weightFreshArray);

                //清空所有行
                weightDatagrid.clearRows();
                // 重新绑定
                for(let obj of weightDatagridList){
                    weightDatagrid.addRow(obj);
                }
            }

            function deleteWeightDatagrid(){
                let selectedItemArray = weightDatagrid.getSelecteds();
                // 删除必须是从后向前删除，而且不能跳行
                // 下面开始检测选择中的是不是连续的
                let deleteWeightGeneralArray = [];
                let deleteWeightFreshArray = [];

                // 完成选择数据的根据货物类型的分离
                for(let obj of selectedItemArray){
                    if(obj.goodsTypeId == goodsType.general){
                        deleteWeightGeneralArray.push(obj);
                    }
                    else{
                        deleteWeightFreshArray.push(obj);
                    }
                }

                // 排序
                if(deleteWeightGeneralArray.length>1){
                    deleteWeightGeneralArray.sort(sortData);
                }
                if(deleteWeightFreshArray.length>1){
                    deleteWeightFreshArray.sort(sortData);
                }

                // 分别检测是否有跳行,但需要注意有没有选中最后一行
                let offset = 0;
                let weightEqualFlag = true;
                for(let i = deleteWeightGeneralArray.length - 1 ;i>=0;i--){
                    offset = offset + 1;
                    let obj = deleteWeightGeneralArray[i];
                    let oldObj = weightGeneralArray[weightGeneralArray.length - offset];
                    weightEqualFlag = equalObj(obj,oldObj);
                    if(!weightEqualFlag){
                        break;
                    }

                }

                if(!weightEqualFlag){
                    showAlert("选择要删除的重量运价标准行不是从后至前连续的，为了数据完整性不能进行删除！");
                    return;
                }


                let freshEqualFlag = true;
                offset = 0;
                for(let i = deleteWeightFreshArray.length - 1 ;i>=0;i--){
                    offset = offset + 1;
                    let obj = deleteWeightFreshArray[i];
                    let oldObj = weightFreshArray[weightFreshArray.length-offset];
                    // 判断是否相等
                    freshEqualFlag = equalObj(obj,oldObj);
                    if(!freshEqualFlag){
                        break;
                    }
                }

                if(!freshEqualFlag){
                    showAlert("选择要删除的重量运价标准行不是从后至前连续的，为了数据完整性不能进行删除！");
                    return;
                }


                let array1 = [];
                let array2 = [];

                if(deleteWeightGeneralArray.length>0){
                    array1 = arraySub(weightGeneralArray,deleteWeightGeneralArray);
                    if(array1.length>1){
                        array1.sort(sortData);
                    }
                }
                else{
                    array1.push(...weightGeneralArray);
                }

                if(deleteWeightFreshArray.length>0){
                    array2 = arraySub(weightFreshArray,deleteWeightFreshArray);
                    if(array2.length>1){
                        array2.sort(sortData);
                    }
                }
                else{
                    array2.push(...weightFreshArray);
                }


                // 清空数组
                weightDatagridList.splice(0,weightDatagridList.length);
                weightGeneralArray.splice(0,weightGeneralArray.length);
                weightFreshArray.splice(0,weightFreshArray.length);

                weightDatagridList.push(...array1);
                weightDatagridList.push(...array2);

                //清空所有行
                weightDatagrid.clearRows();
                for(let obj of weightDatagridList){
                    // 重新初始化现有数据行
                    if(obj.goodsTypeId == goodsType.general){
                        weightGeneralArray.push(obj);
                    }
                    else{
                        weightFreshArray.push(obj);
                    }
                    weightDatagrid.addRow(obj);
                }
            }

            /**
             * 添加体积分类协议运价
             */
            function addVolumeDatagrid() {
                if(isEmpty(volumeGoodsTypeId,"货物类型")){
                    return;
                }

                if(isEmpty(volumeStart,"体积起始值")){
                    return;
                }

                if(isEmpty(volumeEnd,"体积结束值")){
                    return;
                }

                if(isEmpty(volumePrice,"体积单价")){
                    return;
                }

                if(isNonNumeric(volumeStart,"体积起始值")){
                    return;
                }

                if(isNonNumeric(volumeEnd,"体积结束值")){
                    return;
                }

                if(isNonNumeric(volumePrice,"体积单价")){
                    return;
                }

                let volumeGoodsTypeIdValue = volumeGoodsTypeId.getValue();
                let volumeStartValue = public_toNumber(volumeStart.getValue());
                let volumeEndValue = public_toNumber(volumeEnd.getValue());
                let volumePriceValue = public_toNumber(volumePrice.getValue());
                let commentValue = volumeComment.getValue();
                let standardTypeIdValue = freightStandard.volume;

                if(volumeEndValue <= 0){
                    showAlert("重量结束值不能小于或等于零!");
                    return;
                }
                else if(volumeEndValue <= volumeStartValue){
                    showAlert("重量结束值不能小于或等于重量起始值!");
                    return;
                }
                if(volumePriceValue <= 0){
                    showAlert("重量单价不能小于或等于零!");
                    return;
                }

                // 生成新添加的对象
                let newRow = {goodsTypeId:volumeGoodsTypeIdValue, startValue:volumeStartValue, endValue:volumeEndValue, price:volumePriceValue, comment:commentValue,standardTypeId:standardTypeIdValue, agreementFreightTypeId:transportSpecies.auto};

                if(volumeDatagridList.length==0){
                    volumeDatagrid.addRow(newRow);
                    volumeDatagridList.push(newRow);

                    if(newRow == goodsType.general){
                        volumeGeneralArray.push(newRow);
                    }
                    else{
                        volumeFreshArray.push(newRow);
                    }
                    return;
                }

                // 拼接函数(索引位置, 要删除元素的数量, 元素) array.splice(2, 0, "three");
                // let goodsType = {general:51, fresh:52};
                // let freightStandard = {volume:41, volume:42};
                // volumeGoodsTypeIdValue

                volumeGeneralArray.splice(0,volumeGeneralArray.length);
                volumeFreshArray.splice(0,volumeFreshArray.length);

                for(let obj of volumeDatagridList){
                    if(obj.goodsTypeId == goodsType.general){
                        volumeGeneralArray.push(obj);
                    }
                    else{
                        volumeFreshArray.push(obj);
                    }
                }

                // 这个地方要检查在现在的列表中现有的对象是否已经存在
                if(volumeGoodsTypeIdValue == goodsType.general){
                    let repetitionFlag = checkRepetition(volumeGeneralArray,newRow);
                    if(!repetitionFlag){
                        volumeGeneralArray.push(newRow);
                    }
                    else{
                        showAlert("新添加的运价标准在现有运价标准中已经存在,不能重复添加!");
                        return;
                    }
                }
                else{
                    let repetitionFlag = checkRepetition(volumeFreshArray,newRow);
                    if(!repetitionFlag){
                        volumeFreshArray.push(newRow);
                    }
                    else{
                        showAlert("新添加的运价标准在现有运价标准中已经存在,不能重复添加!");
                        return;
                    }
                }

                // 两个数组分别按起始值进行排序
                if(volumeGeneralArray.length>1){
                    volumeGeneralArray.sort(sortData);
                }
                if(volumeFreshArray.length>1){
                    volumeFreshArray.sort(sortData);
                }

                // 检测是否符合对应的要求
                if(volumeGeneralArray.length>=2){
                    let volumeGeneralCheckFlag = sequenceCheck(volumeGeneralArray);
                    if(!volumeGeneralCheckFlag){
                        showAlert("新添加的运价标准不符合前一条重量结束值与下一条重量起始值相同的要求!");
                        return;
                    }
                }
                if(volumeFreshArray.length>=2){
                    let volumeFreshCheckFlag = sequenceCheck(volumeFreshArray);
                    if(!volumeFreshCheckFlag){
                        showAlert("新添加的运价标准不符合前一条体积结束值与下一条体积起始值相同的要求!");
                        return;
                    }
                }

                // 清空数组
                volumeDatagridList.splice(0,volumeDatagridList.length);

                volumeDatagridList.push(...volumeGeneralArray);
                volumeDatagridList.push(...volumeFreshArray);

                //清空所有行
                volumeDatagrid.clearRows();
                // 重新绑定
                for(let obj of volumeDatagridList){
                    volumeDatagrid.addRow(obj);
                }
            }

            /**
             * 删除体积协议运价
             */
            function deleteVolumeDatagrid() {
                let selectedItemArray = volumeDatagrid.getSelecteds();
                // 删除必须是从后向前删除，而且不能跳行
                // 下面开始检测选择中的是不是连续的
                let deleteVolumeGeneralArray = [];
                let deleteVolumeFreshArray = [];

                // 完成选择数据的根据货物类型的分离
                for(let obj of selectedItemArray){
                    if(obj.goodsTypeId == goodsType.general){
                        deleteVolumeGeneralArray.push(obj);
                    }
                    else{
                        deleteVolumeFreshArray.push(obj);
                    }
                }

                // 排序
                if(deleteVolumeGeneralArray.length>1){
                    deleteVolumeGeneralArray.sort(sortData);
                }
                if(deleteVolumeFreshArray.length>1){
                    deleteVolumeFreshArray.sort(sortData);
                }

                // 分别检测是否有跳行,但需要注意有没有选中最后一行
                let offset = 0;
                let volumeEqualFlag = true;
                for(let i = deleteVolumeGeneralArray.length - 1 ;i>=0;i--){
                    offset = offset + 1;
                    let obj = deleteVolumeGeneralArray[i];
                    let oldObj = volumeGeneralArray[volumeGeneralArray.length - offset];
                    volumeEqualFlag = equalObj(obj,oldObj);
                    if(!volumeEqualFlag){
                        break;
                    }

                }

                if(!volumeEqualFlag){
                    showAlert("选择要删除的体积运价行标准不是从后至前连续的，为了数据完整性不能进行删除！");
                    return;
                }

                let freshEqualFlag = true;
                offset = 0;
                for(let i = deleteVolumeFreshArray.length - 1 ;i>=0;i--){
                    offset = offset + 1;
                    let obj = deleteVolumeFreshArray[i];
                    let oldObj = volumeFreshArray[volumeFreshArray.length-offset];
                    // 判断是否相等
                    freshEqualFlag = equalObj(obj,oldObj);
                    if(!freshEqualFlag){
                        break;
                    }
                }

                if(!freshEqualFlag){
                    showAlert("选择要删除的体积运价行标准行不是从后至前连续的，为了数据完整性不能进行删除！");
                    return;
                }

                let array1 = [];
                let array2 = [];

                if(deleteVolumeGeneralArray.length>0){
                    array1 = arraySub(volumeGeneralArray,deleteVolumeGeneralArray);
                    if(array1.length>1){
                        array1.sort(sortData);
                    }
                }
                else{
                    array1.push(...volumeGeneralArray);
                }

                if(deleteVolumeFreshArray.length>0){
                    array2 = arraySub(volumeFreshArray,deleteVolumeFreshArray);
                    if(array2.length>1){
                        array2.sort(sortData);
                    }
                }
                else{
                    array2.push(...volumeFreshArray);
                }

                // 清空数组
                volumeDatagridList.splice(0,volumeDatagridList.length);
                volumeGeneralArray.splice(0,volumeGeneralArray.length);
                volumeFreshArray.splice(0,volumeFreshArray.length);

                volumeDatagridList.push(...array1);
                volumeDatagridList.push(...array2);

                //清空所有行
                volumeDatagrid.clearRows();
                for(let obj of volumeDatagridList){
                    // 重新初始化现有数据行
                    if(obj.goodsTypeId == goodsType.general){
                        volumeGeneralArray.push(obj);
                    }
                    else{
                        volumeFreshArray.push(obj);
                    }
                    volumeDatagrid.addRow(obj);
                }
            }

            function closePanel(panelId) {
                // 先清空

                // 再关闭
                let winObj = mini.get(panelId);
                winObj.hide();
            }


            // 关闭或保存后的数据清空
            function emptyValue() {
                opFlag.setValue();
                customerId.setValue();
                cityId.setValue();
                comment.setValue();

                weightGoodsTypeId.setValue();
                weightStart.setValue();
                weightEnd.setValue();
                weightPrice.setValue();
                weightComment.setValue();

                volumeGoodsTypeId.setValue();
                volumeStart.setValue();
                volumeEnd.setValue();
                volumePrice.setValue();
                volumeComment.setValue();

                emptyDataAndGrid();

            }

            function emptyDataAndGrid() {
                volumeDatagridList.splice(0,volumeDatagridList.length);
                volumeGeneralArray.splice(0,volumeGeneralArray.length);
                volumeFreshArray.splice(0,volumeFreshArray.length);
                //清空所有行
                volumeDatagrid.clearRows();


                weightDatagridList.splice(0,weightDatagridList.length);
                weightGeneralArray.splice(0,weightGeneralArray.length);
                weightFreshArray.splice(0,weightFreshArray.length);
                //清空所有行
                weightDatagrid.clearRows();
            }



            /**
             * 类型及开始与结束值相等就可以了
             */
            function equalObj(obj1,obj2){
                return obj1.goodsTypeId === obj2.goodsTypeId && obj1.startValue === obj2.startValue && obj1.endValue === obj2.endValue;
            }

            /**
             * 对象根据类型及起始值进行排序
             * @param obj1
             * @param obj2
             * @returns {number}
             */
            function sortData(obj1, obj2) {
                return obj1.startValue - obj2.startValue;
            }

            /**
             * 序列检测
             * @param checkArray
             * @returns {boolean}
             */
            function sequenceCheck(checkArray){
                let flag = true;
                for(let i=0;i<checkArray.length -1;i++){
                    let value1 = checkArray[i].endValue;
                    let value2 = checkArray[i+1].startValue;
                    if(value1!=value2){
                        flag = false;
                        break;
                    }
                }
                return flag;
            }

            /**
             * 检测对象在数组对象中是否已经存在
             * @param array
             * @param item
             * @returns {boolean}
             */
            function checkRepetition(array,item) {
                let repetitionFlag = false;
                for(let obj of array){
                    repetitionFlag = equalObj(obj,item);
                    if(repetitionFlag){
                        break;
                    }
                }
                return repetitionFlag;
            }

            /**
             * 求差集，返回的是一个新的对象
             */
            function arraySub(objArray1,objArray2){
                let resultArray = [];
                for(let i=0;i<objArray1.length;i++){
                    let obj1 = objArray1[i];
                    let flag = false;
                    for(let j=0;j<objArray2.length;j++){
                        let obj2 = objArray2[j];
                        flag = equalObj(obj1,obj2);
                        if(flag){
                            break;
                        }
                    }
                    if(!flag){
                        resultArray.push(obj1);
                    }
                }
                return resultArray;
            }


        </script>

    </body>
</html>